package com.czk.getApplcationContext;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;

/**
 * 1 实现BeanFactoryAware接口
 *
 * @Author:ChenZhangKun
 * @Date: 2021/4/28 14:54
 */
@Component
public class BeanFactoryUtils implements BeanFactoryAware {
    private static BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("beanFactory方法被调用了");
        BeanFactoryUtils.beanFactory = beanFactory;
    }

    public static <T> T getBean(Class<T> clazz) {
        return BeanFactoryUtils.beanFactory.getBean(clazz);
    }
}
